﻿package com.kuanter.kuanterauto.activity;

import android.content.ActivityNotFoundException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.kuanter.kuanterauto.R;
import com.kuanter.kuanterauto.activity.setting.AboutUsActivity;
import com.kuanter.kuanterauto.activity.setting.UseIntroActivity;
import com.kuanter.kuanterauto.baseui.ProApplication;
import com.kuanter.kuanterauto.dataservices.PushServices;
import com.kuanter.kuanterauto.utils.DrawNumber;
import com.kuanter.kuanterauto.utils.SharePrefenceUtil;
/**
 * 设置
 * @author kuanter
 *
 */
public class SettingActivity extends WuyangBaseActivity {

	private TextView setting_push_set_title;
	private TextView setting_website_default_show_title;
	private TextView setting_mark_title;
	private TextView setting_feedback_title;
	private TextView setting_service_manual_title;
	private TextView setting_about_us_title;
	private TextView setting_my_message_title;
	private TextView setting_bound_set_title;
	private Button back_btn;
	private View setting_push_set;
	private View setting_website_default_show;
	private View setting_mark;
	private View setting_feedback;
	private View setting_service_manual;
	private View setting_about_us;
	private View setting_my_message;
	private View setting_bound_set;
	private ImageView Star_weixin;
	private ImageView Star_xinlang;
	
	private boolean push_flag = false ;
	private int website_flag;
	private Object flagswitch ;
	private ImageView push_set_iv;
	private ImageView website_defaultlist_show_set_iv;
	private Intent service;
	private ProApplication proApplication;
	protected Editor editor;
	private WebView webView;
	private ImageView bubble;
	private DrawNumber drawNumber;
	
	GetMessage rece;
	
	LayoutInflater factory;
	
	/*@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		int messageNumber = 0;
		if(null != PushServices.getmessageInfos()){
			messageNumber = PushServices.getmessageInfos().size();
		}
		Bitmap bitmap = drawNumber.generatorContactCountIcon(drawNumber.convertViewToBitmap(bubble),messageNumber);
		bubble.setImageBitmap(bitmap);
		super.onResume();
	}*/
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		setContentView(R.layout.setting);
		super.onCreate(savedInstanceState);
		
		factory = getLayoutInflater();
		
		this.setHeaderControlViewText();
		this.initBodyControlView();
		this.setBodyControlViewText();
		this.registerListener();
		rece = new GetMessage();
		IntentFilter filter = new IntentFilter("com.setting.show");
		registerReceiver(rece, filter);
		
		push_flag = SharePrefenceUtil.getMessagePush(this);
		if(push_flag == false){
			push_set_iv.setImageResource(R.drawable.btn_switch_pressed);
//			SharePrefenceUtil.setMessagePush(this, true);
		}else {
			push_set_iv.setImageResource(R.drawable.btn_switch_normal);
//			SharePrefenceUtil.setMessagePush(this,false);
		}
		website_flag = SharePrefenceUtil.getRotateStatus(SettingActivity.this);
		if(website_flag > 0){
			website_defaultlist_show_set_iv.setImageResource(R.drawable.btn_switch_normal);
			SharePrefenceUtil.setRotateStatus(SettingActivity.this,1);
		}else if(website_flag<0){
			website_defaultlist_show_set_iv.setImageResource(R.drawable.btn_switch_pressed);
			SharePrefenceUtil.setRotateStatus(SettingActivity.this,-1);
		}else {
			website_defaultlist_show_set_iv.setImageResource(R.drawable.btn_switch_normal);
		}
		bubble = (ImageView)this.findViewById(R.id.bubble);
		drawNumber = new DrawNumber(this);
		/*//获取新消息数
		int messageNumber = PushServices.unReadMessage;
		View v = factory.inflate(R.layout.draw_number, null);
//		if(null != PushServices.getmessageInfos()){
//			messageNumber = PushServices.getmessageInfos().size();
			if(messageNumber > 0){
				bubble.setVisibility(View.VISIBLE);
				Bitmap bitmap = drawNumber.generatorContactCountIcon(drawNumber.convertViewToBitmap(v),messageNumber);
				bubble.setImageBitmap(bitmap);
			}else{
				bubble.setVisibility(View.GONE);
			}*/
//		}
		/*else{
			bubble.setVisibility(View.GONE);*/
//		}
	}
	@Override
	protected void setHeaderControlViewText() {
		// TODO Auto-generated method stub
		super.setHeaderTitleText(super.headerTitle, "设置");
		super.setHeaderLeftText(super.headerLeft, null);
		super.setHeaderRightText(super.headerRight, null);
	}

	@Override
	protected void initBodyControlView() {
		// TODO Auto-generated method stub
		//推送
		setting_push_set = this.findViewById(R.id.setting_push_set);
		setting_push_set_title = (TextView)setting_push_set.findViewById(R.id.include_image_1tv_image_TV);
		push_set_iv = (ImageView)setting_push_set.findViewById(R.id.setting_switch);
		//网点显示方式
		setting_website_default_show = this.findViewById(R.id.setting_website_default_show);
		setting_website_default_show_title =  (TextView)setting_website_default_show.findViewById(R.id.include_image_1tv_image_TV);
		website_defaultlist_show_set_iv = (ImageView)setting_website_default_show.findViewById(R.id.setting_switch);
		//关注我们
		Star_weixin = (ImageView)this.findViewById(R.id.Star_weixin) ;
		Star_xinlang = (ImageView)this.findViewById(R.id.Star_xinlang) ;
		//打分
		setting_mark = this.findViewById(R.id.setting_mark);
		setting_mark_title = (TextView)setting_mark.findViewById(R.id.include_image_1tv_arrow_TV);
		//用户反馈
		setting_feedback = this.findViewById(R.id.setting_feedback);
//		setting_feedback.setBackgroundResource(R.drawable.item_top_selector);
		setting_feedback_title = (TextView)setting_feedback.findViewById(R.id.include_image_1tv_arrow_TV);
		//服务手册
		setting_service_manual = this.findViewById(R.id.setting_service_manual);
		setting_service_manual.setBackgroundResource(R.drawable.item_middle_selector);
		setting_service_manual_title = (TextView)setting_service_manual.findViewById(R.id.include_image_1tv_arrow_TV);
		//关于我们
		setting_about_us = this.findViewById(R.id.setting_about_us);
		setting_about_us.setBackgroundResource(R.drawable.item_bottom_selector);
		setting_about_us_title =  (TextView)setting_about_us.findViewById(R.id.include_image_1tv_arrow_TV);
		// 我的消息
		setting_my_message = this.findViewById(R.id.setting_my_message);
		setting_my_message_title = (TextView)setting_my_message.findViewById(R.id.include_image_1tv_drawable_right_image_TV);
		// 绑定设置
		setting_bound_set = this.findViewById(R.id.setting_bound_set);
		setting_bound_set_title = (TextView)setting_bound_set.findViewById(R.id.include_image_1tv_arrow_TV);
		//返回按钮
		back_btn = (Button)this.findViewById(R.id.header_left);
	}

	@Override
	protected void setBodyControlViewText() {
		// TODO Auto-generated method stub
		setting_push_set_title.setText("免打扰设置");
		setting_website_default_show_title.setText("网点默认列表显示");
		setting_mark_title.setText("亲!给我们打个分！");
		setting_feedback_title.setText("用户反馈");
		setting_service_manual_title.setText("使用说明");
		setting_about_us_title.setText("关于我们");
		setting_my_message_title.setText("我的消息");
		setting_bound_set_title.setText("绑定设置");
	}

	@Override
	protected void registerListener() {
		// TODO Auto-generated method stub
		back_btn.setOnClickListener(this);
		//
		Star_weixin.setOnClickListener(this);
		Star_xinlang.setOnClickListener(this);
		
		//打分、
		setting_mark.setOnClickListener(this);
		setting_feedback.setOnClickListener(this);
		setting_service_manual.setOnClickListener(this);
		setting_about_us.setOnClickListener(this);
		setting_my_message.setOnClickListener(this);
		setting_bound_set.setOnClickListener(this);
		push_set_iv.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(push_flag == false){
					push_flag = true ;
					push_set_iv.setImageResource(R.drawable.btn_switch_normal);
					SharePrefenceUtil.setMessagePush(SettingActivity.this, true);
					Intent service = new Intent(SettingActivity.this,PushServices.class);
					startService(service);
				}else {
					push_flag = false ;
					push_set_iv.setImageResource(R.drawable.btn_switch_pressed);
					SharePrefenceUtil.setMessagePush(SettingActivity.this, false);
					Intent service = new Intent(SettingActivity.this,PushServices.class);
					stopService(service);
				}
			}
		});
		website_defaultlist_show_set_iv.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				boolean flag = website_flag > 0?true:false ;
				if(flag){
					website_flag = -1;
					website_defaultlist_show_set_iv.setImageResource(R.drawable.btn_switch_pressed);
					SharePrefenceUtil.setRotateStatus(SettingActivity.this,-1);
				}else {
					website_flag = 1 ;
					website_defaultlist_show_set_iv.setImageResource(R.drawable.btn_switch_normal);
					SharePrefenceUtil.setRotateStatus(SettingActivity.this,1);
				}
			}
		});
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.header_left:
			this.finish();
			break;
		case R.id.setting_switch:
			
			
			//推送设置处理
			
			//待定
			break;
		case R.id.Star_weixin:
			/*Toast.makeText(this, "操作:朋友们->添加朋友", Toast.LENGTH_LONG);
			Intent intent = new Intent();
			ComponentName componentName = new ComponentName("com.tencent.mm","com.tencent.mm.ui.LauncherUI");
			intent.setComponent(componentName);
			//intent.setAction("android.intent.action.VIEW");
			startActivity(intent);*/
			Intent intent = new Intent("android.intent.action.VIEW");
//			intent.setData(Uri.parse("http://weixin.qq.com/r/8HQ7NLHEY48ch6BXnyHU"));
			intent.setData(Uri.parse("http://weixin.qq.com/r/NHXo5ybEphjZhzeEnyAQ"));
			startActivity(intent);
			break;
		case R.id.Star_xinlang:
//			showToast("关注");
			Intent intent2 = new Intent(this,AttentionXinLangWeiboActivity.class);
			startActivity(intent2);
			break;
		case R.id.setting_mark:
			Uri uri = Uri.parse("market://details?id=" + this.getPackageName());
			Intent intentMarket = new Intent(Intent.ACTION_VIEW,uri);
			try{
				startActivity(intentMarket);
			}catch(ActivityNotFoundException e){
				Toast.makeText(this, "Couldn't launch the market !", Toast.LENGTH_SHORT).show();
			}
			break;
		case R.id.setting_feedback:
			super.startMyActivity(this, UserFeedbackActivity.class);
			break;
		case R.id.setting_service_manual:
//			startMyActivity(this, SettingServiceActivity.class);
			startMyActivity(this, UseIntroActivity.class);
			break;
		case R.id.setting_about_us:
			startMyActivity(this, AboutUsActivity.class);
			break;
		case R.id.setting_my_message:
			//我的消息
			//showToast("我的消息");
			intent = new Intent(this,MyMessageActivity.class);
			startActivity(intent);
			break;
		case R.id.setting_bound_set:
//			showToast("绑定设置");
			Intent intent3 = new Intent(this,BoundSettingActivity.class);
			startActivity(intent3);
			break;
		default:
			break;
		}
	}
	@Override
	protected void getControlViewText() {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	protected void onDestroy() {
		unregisterReceiver(rece);
		super.onDestroy();
	}



	class GetMessage extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			//获取新消息数
			int messageNumber = PushServices.unReadMessage;
			DrawNumber drawNumber = new DrawNumber(SettingActivity.this);
			
			View v = factory.inflate(R.layout.draw_number, null);
//			if(null != PushServices.getmessageInfos()){
//				messageNumber = PushServices.getmessageInfos().size();
				if(messageNumber > 0){
					bubble.setVisibility(View.VISIBLE);
					Bitmap bitmap = drawNumber.generatorContactCountIcon(drawNumber.convertViewToBitmap(v),messageNumber);
					bubble.setImageBitmap(bitmap);
				}else{
					bubble.setVisibility(View.GONE);
				}
//			}
			/*else{
				bubble.setVisibility(View.GONE);*/
//			}
		}
		
	}

}
